<?php

namespace Home\Controller;

use Doctrine\Common\Inflector\Inflector;
use Faker\Factory;
use Think\Controller;

class SamplesController extends Controller
{
    public function index()
    {
        /*
        echo Inflector::pluralize('summary');
        echo "<br>";
        echo Inflector::singularize('summaries');
        echo "<br>";
        echo Inflector::tableize('currentPage');
        echo "<br>";
        echo Inflector::camelize('current page');
        */

        $faker = Factory::create();
        $sales = [];
        for($i = 0; $i < 10; $i++ ) {
            array_push($sales,[
                'value' => $faker->numberBetween(1,20),
                'name' => $faker->lastName
            ]);
        }

        $samples = [];
        for ($i = 0; $i < $faker->numberBetween(1,10); $i++) {
            $samples[] = [
                'sample_id' => $faker->randomDigit,
                'sample_order' => $faker->words(2,true),
                'sample_type' => $faker->numberBetween(1,2),
                'sample_progress' => $faker->numberBetween(0, 100),
                'sales_face' => $faker->imageUrl(40,40),
                'sales_name' => $faker->lastName,
                'create_time' => $faker->unixTime
            ];
        }
        foreach ($samples as $key => &$sample) {
            //给$sample数组中每一个子数据新添一个属性products
            for ($i = 0; $i < $faker->numberBetween(1,5); $i++) {
                $sample['details'][] = [
                    'product_id' => $faker->randomDigit,
                    'product_type' => $faker->word,
                    'product_model' => $faker->words(3,true),
                    'product_progress' => [
                        'progress_value' => $faker->numberBetween(1,12),
                        'progress_label' => $faker->word,
                    ]
                ];
            }
        }

        $this->assign('sales',$sales);
        $this->assign('samples',$samples);
        $this->display();
    }

    public function create()
    {
        $faker = Factory::create();
        $brand = [];
        for($i = 0; $i < 10; $i++ ) {
            array_push($brand,[
                'value' => $faker->numberBetween(1,20),
                'name' => $faker->lastName
            ]);
        }
        $this->assign('brand',$brand);
        $this->display();
    }

    public function show()
    {
        $faker = Factory::create();
        //封面
        $product = [
            'sample_id' => $faker->randomDigit, //订单id
            'sample_order' => $faker->words(2,true),
            'product_type' => $faker->word,                       //产品类型
            'product_model' => $faker->words(3,true),  //产品型号
            'manager_id' => $faker->numberBetween(1,100),
            'manager_name' => $faker->lastName,
            'count' => $faker->numberBetween(1,100),
            'customer' => $faker->lastName,
            'brand' => $faker->word,
            'model' => $faker->word,
            'remark' => $faker->text(200),
            'request_delivery' => $faker->unixTime,
            'expected_delivery' => $faker->unixTime,
            'actual_delivery' => $faker->unixTime,
            'product_progress' => [
                'progress_value' => $faker->numberBetween(1,12),
                'progress_label' => $faker->word,
            ]
        ];
        //判断是否延期
        if (strtotime(date("Y-m-d",$product['actual_delivery'])) > strtotime(date("Y-m-d",$product['request_delivery']))) {
            $is_delay = 1;
        } else {
            $is_delay = -1;
        }
        $product['is_delay'] = $is_delay;

        //工作流程
        $progress = [];
        for ($i = 0; $i < 11; $i++) {
            array_push($progress,[
                'step_id' => $i+1,
                'step_name' => $faker->word,
                'step_status' => $faker->numberBetween(0,3)
            ]);
        }
        foreach ($progress as $key => &$pro) {
            for ($i = 0; $i < $faker->numberBetween(1,4); $i++) {
                $pro['step_info'][] = [
                    'face' => $faker->imageUrl(40,40),
                    'name' => $faker->lastName,
                    'view' => $faker->text(100),
                    'time' => $faker->unixTime
                ];
            }
        }

        //物流信息
        $logistics = [
            'company' => '圆通快递',
            'company_order' => 'YTO',
            'order' => 'YT4002624443229',
            'create_user' => $faker->lastName,
            'create_time' => $faker->unixTime
        ];

        //操作日志
        $log = [];
        for ($i = 0; $i < $faker->numberBetween(1,10); $i++) {
            array_push($log,[
                'name' => $faker->lastName,
                'action' => $faker->text(100),
                'step' => $faker->word,
                'time' => $faker->unixTime
            ]);
        }

        //测试报告
        $files = [];
        for ($i = 0; $i < $faker->numberBetween(1,3); $i++) {
            array_push($files,[
                'name' => $faker->words(3, true),
                'ext' => 'xls'
            ]);
        }

        //操作详细
        $detailResult = [
            'now_step' => $faker->numberBetween(1,11),
            'max_step' => $faker->numberBetween(11,11),
            'termination' => $faker->randomElement(['Y', 'N']),
            'rollback' => $faker->randomElement(['Y', 'N']),
            'transfer' => $faker->randomElement(['Y', 'N']),
            'extension' => $faker->randomElement(['Y', 'N']),
            'manager' => $faker->numberBetween(0, 100)
        ];
        $detailResult['next_step'] = [
            'id' => $detailResult['now_step']+1,
            'name' => $progress[$detailResult['now_step']]['step_name']
        ];
        $detailResult['prev_step'] = [
            'id' => $detailResult['now_step']-1,
            'name' => $progress[$detailResult['now_step']-2]['step_name']
        ];
        $user = [];
        for ($i = 0; $i < $faker->numberBetween(10, 100); $i++) {
            array_push($user,[
                'id' => $faker->numberBetween(0, 100),
                'name' => $faker->lastName
            ]);
        }
        $persons = $user;
        $transfer = $user;
        $rollback = $user;
        $ccList = $user;

        $logistic = [];
        for ($i = 0; $i < $faker->numberBetween(10, 100); $i++) {
            array_push($logistic,[
                'id' => $faker->numberBetween(0, 100),
                'name' => $faker->company
            ]);
        }


        $this->assign("product",$product);
        $this->assign('progress',$progress);
        $this->assign('logistics',$logistics);
        $this->assign('log',$log);
        $this->assign('files',$files);
        $this->assign('detailResult',$detailResult);
        $this->assign('persons',$persons);
        $this->assign('transfer',$transfer);
        $this->assign('rollback',$rollback);
        $this->assign('ccList',$ccList);
        $this->assign('logistic',$logistic);
        $this->display();
    }

    public function overview()
    {
        $faker = Factory::create();

        $details = [];
        for ($i = 0; $i < $faker->numberBetween(1,5); $i++) {
            $details[] = [
                'id' => $faker->randomDigit,   //产品id
                'product_type' => $faker->word,   //产品类型
                'product_model' => $faker->words(3,true),  //产品型号
                'manager_id' => $faker->numberBetween(1,100),
                'manager_name' => $faker->lastName,
                'count' => $faker->numberBetween(1,100),
                'customer' => $faker->lastName,
                'brand' => $faker->word,
                'model' => $faker->word,
                'product_progress' => [
                    'progress_value' => $faker->numberBetween(1,11),
                    'progress_label' => $faker->word,
                ]
            ];
        }

        $files = [];
        for ($i = 0; $i < $faker->numberBetween(1,5); $i++) {
            $files[] = [
                'file_title' => $faker->words(3,true),
                'file_type' => $faker->numberBetween(1,6)
            ];
        }

        $samples = [
            'sample_order' => $faker->words(3, true),
            'sample_type' => $faker->numberBetween(1,2),
            'sample_progress' => $faker->numberBetween(0, 100),
            'sales_name' => $faker->lastName,
            'create_time' => $faker->unixTime,
            'update_time' => $faker->unixTime,
            'start_time' => $faker->unixTime,
            'complete_time' => $faker->unixTime,
            'details' => $details,
            'files' => $files,
            'id' => $faker->randomDigit
        ];

        $samples['total_time'] = $samples['complete_time'] - $samples['start_time'];

        $this->assign('samples', $samples);
        $this->display();
    }

    public function edit()
    {
        $faker = Factory::create();
        $details=[];
        for ($i = 0; $i < $faker->numberBetween(1,4); $i++) {
            //循环依次给该数组赋值一套数据
            $details[] = [
                'product_type' => $faker->word,   //产品类型
                'product_model' => $faker->words(3,true),  //产品型号
                'manager_id' => $faker->numberBetween(1,100),
                'manager_name' => $faker->lastName,
                'count' => $faker->numberBetween(1,100),
                'customer' => $faker->lastName,
                'brand' => $faker->numberBetween(1,100),
                'model' => $faker->word,
                'remark' => $faker->text(200),
                'request_delivery' => $faker->unixTime,
            ];
        }
        $samples=[
            'sample_order' => $faker->words(3,true),
            'sample_type' => $faker->randomElement(['免费', '付费']),
            'details' => $details
        ];

        $brand = [];
        for($i = 0; $i < 10; $i++ ) {
            array_push($brand,[
                'value' => $faker->numberBetween(1,20),
                'name' => $faker->lastName
            ]);
        }

        $this->assign('brand',$brand);
        $this->assign('samples',$samples);
        $this->display();
    }

    public function summaries()
    {
        $faker = Factory::create();

        $samples = [];
        for ($i = 0; $i < $faker->numberBetween(1,10); $i++) {
            $samples[] = [
                'sample_order' => $faker->words(4, true),
                'sales_name' => $faker->lastName
            ];
        }
        foreach ($samples as $key => &$sample) {
            //给$sample数组中每一个子数据新添一个属性products
            for ($i = 0; $i < $faker->numberBetween(1,5); $i++) {
                $sample['details'][] = [
                    'product_type' => $faker->word,
                    'product_model' => $faker->sentence(4, true),
                    'remark' => $faker->text(200),
                    'brand' => $faker->word,
                    'model' => $faker->word,
                    'product_progress' => [
                        'progress_value' => $faker->numberBetween(1,10),
                        'progress_label' => $faker->word,
                    ]
                ];
            }
        }

        $this->assign('samples',$samples);
        $this->display('Samples/summaries/index');
    }

    /*public function summary()
    {
        $this->display('Samples/summaries/show');
    }

    public function summaryCreate()
    {
        $this->display('Samples/summaries/create');
    }

    public function summaryEdit()
    {
        $this->display('Samples/summaries/edit');
    }*/
}